﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace The_LarX
{
    public static class FloatUtil
    {
        /// <summary>
        /// Converts a float to string with the desired number
        /// of decimals
        /// </summary>
        /// <param name="decimalHouses"></param>
        /// <returns></returns>
        public static string ToString(float fl, int decimalHouses)
        {
            string s = fl.ToString();
            int point = s.IndexOf(",");
            if (point == -1)
                point = s.IndexOf(".");
            if (point == -1)
                return fl.ToString();

            if (s.Length > point + decimalHouses)
                s = s.Remove(point + decimalHouses);
            return s;
        }

        public static float CreateFromString(string str)
        {
            return float.Parse(str, CultureInfo.InvariantCulture);
        }

        public static string ToSerialiableString(float f)
        {
            return "Float:" + f.ToString(CultureInfo.InvariantCulture);
        }
        public static float FromSerializableString(string str)
        {
            if (str.StartsWith("Float:"))
            {
                string[] floatValues = str.Split(":".ToCharArray());
                float f = float.Parse(floatValues[1], CultureInfo.InvariantCulture);
                return f;
            }
            throw new Exception("Value is not on Serialiazation Standards. Use ToSerialiableString method from this class" +
                " to make Serialiable Floats");
        }
    }
}
